package com.infore.airsupervise.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.MissionE;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @features: 流程显示fragment
 * @author: WuTao
 * @date: 2017/5/10 08:57
 */
public class MissionProcessFragment extends BaseFragment {
    @BindView(R.id.show_photo_recycler_view)
    RecyclerView recyclerView;

    private List<MissionE.MissionProcess> datas;
    private BaseQuickAdapter< MissionE.MissionProcess,BaseViewHolder> adapter;

    public MissionProcessFragment() {
    }

    @SuppressLint("ValidFragment")
    public MissionProcessFragment(List<MissionE.MissionProcess> datas,String time , String pson) {
        if(datas!=null){
            this.datas = datas;
        }
        this.time = time;
        this.pson = pson;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show_photo, container, false);
        unbinder = ButterKnife.bind(this, view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BaseQuickAdapter<MissionE.MissionProcess, BaseViewHolder>(R.layout.item_mission_process,datas) {
            @Override
            protected void convert(BaseViewHolder helper, MissionE.MissionProcess item) {
                helper.setText(R.id.tv_process_title, getStatus(item.getStatus()));
                    String psonName = "";
                    if (null != item.getPson() && null != item.getPson().getName()) {
                        psonName = item.getPson().getName();
                    }
                    if (!TextUtils.isEmpty(item.getTime())) {
                        helper.setText(R.id.tv_process_time, item.getTime());
                    } else {
                        helper.setText(R.id.tv_process_time, "办理中");
                    }
                    helper.setText(R.id.tv_process_pson, psonName);
                    helper.setText(R.id.tv_process_discrib, item.getDescrib());
                        BootstrapLabel icon = helper.getView(R.id.iv_icon);
                        BootstrapText.Builder builder = new BootstrapText.Builder(getContext());
                        builder.addText(psonName.length() == 0 ? psonName : psonName.substring(psonName.length() - 1));
                        icon.setBootstrapText(builder.build());
                        if (item.getStatus() == StatusConst.MISSION_PROCESS_1 ||
                                item.getStatus() == StatusConst.MISSION_PROCESS_5) {
                            icon.setBootstrapBrand(DefaultBootstrapBrand.REGULAR);
                        }else{
                            icon.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                        }
                    }
        };
        recyclerView.setAdapter(adapter);
        addHeaderView();
        return view;
    }

    /**
     * 流程起始
     */
    private String time;
    private String pson;
    public void addHeaderView(){
        View header = LayoutInflater.from(getActivity()).inflate(R.layout.item_mission_process,null);
        BootstrapLabel icon = (BootstrapLabel) header.findViewById(R.id.iv_icon);
        icon.setBootstrapBrand(DefaultBootstrapBrand.INFO);
        TextView tv_title = (TextView) header.findViewById(R.id.tv_process_title);
        TextView tv_name = (TextView) header.findViewById(R.id.tv_process_pson);
        TextView tv_time = (TextView) header.findViewById(R.id.tv_process_time);
        TextView tv_describ = (TextView) header.findViewById(R.id.tv_process_discrib);
        tv_time.setText(time);
        tv_title.setText("任务发起");
        tv_describ.setText("请"+pson+"办理该任务");
        tv_name.setText(pson);
        adapter.addHeaderView(header);
    }
    /**
     * 流程结束
     */
    public void addFooterView(){
        View foot = LayoutInflater.from(getActivity()).inflate(R.layout.item_mission_process,null);
        BootstrapLabel icon = (BootstrapLabel) foot.findViewById(R.id.iv_icon);
        icon.setBootstrapBrand(DefaultBootstrapBrand.INFO);
        TextView tv_title = (TextView) foot.findViewById(R.id.tv_process_title);
        tv_title.setText("结束");
        adapter.addFooterView(foot);
    }

    /**
     * 跟适配器
     */
    public void update(){
        if(null == datas || datas.size()<=0){
            return ;
        }
        int lastStatus = getLastStatus();
        if(lastStatus == StatusConst.MISSION_PROCESS_3 ||
                lastStatus ==StatusConst.MISSION_PROCESS_2  ){
            //流程的尾布局
            addFooterView();
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 获取当前流程
     * @return
     */
    public int getLastStatus(){
        if(datas != null && datas.size()>0){
            return datas.get(datas.size()-1).getStatus();
        }else {
            return -1 ;
        }
    }

    /**
     * 任务状态
     */
    private String getStatus(int status){
        String str_status ="";
        switch (status){
            case StatusConst.MISSION_PROCESS_1 :
            case StatusConst.MISSION_PROCESS_2 :
                str_status = "任务落实";
                break;
            case StatusConst.MISSION_PROCESS_3 :
                str_status = "审核通过";
                break;
            case StatusConst.MISSION_PROCESS_4 :
                str_status = "审核不通过";
                break;
            case StatusConst.MISSION_PROCESS_5 :
                str_status = "审核中";
                break;
        }
        return str_status;
    }

}
